package com.qdxht.dispenser.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import androidx.viewpager.widget.ViewPager;

import com.qdxht.dispenser.base.AppConfig;

import java.lang.reflect.Field;

public class Tools {
    /**
     * 当前网络是否可用
     *
     * @param context
     * @return
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager != null) {
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isConnected()) {
                    // 当前网络是连接的
                    if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
                        // 当前所连接的网络可用
                        return true;
                    }
                }
            }

        }
        return false;
    }


    /**
     * 跳转到系统设置界面
     *
     * @param context
     */
    public static void toSystemSetting(Context context) {
        Intent intent = new Intent(Settings.ACTION_SETTINGS);
        context.startActivity(intent);
    }

    /**
     * 跳转到WIFI设置界面
     *
     * @param context
     */
    public static void toWirelessSetting(Context context) {
        Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
        context.startActivity(intent);
    }


    /**
     * 获取view的图片
     *
     * @param view 要获取图片的view
     * @return
     */
    public static Bitmap convertViewToBitmap(View view) {
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();

        return bitmap;
    }

    /**
     * 获取循环viewpager的真正位置
     *
     * @param size
     * @param position
     * @return
     */
    public static int getLoopViewPagerRealPosition(int size, int position) {
        if (position == 0) {
            position = size - 1;
        } else if (position == size + 1) {
            position = 0;
        } else {
            position = position - 1;
        }
        return position;
    }


    /**
     * re_calculate the height of listview to show all data(it's bug for listview and scrollview)
     *
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    public static int getListViewHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return 0;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            if (listItem != null) {
                try {
                    listItem.measure(0, 0);
                    totalHeight += listItem.getMeasuredHeight();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        int dividerHeight = listView.getDividerHeight() * (listAdapter.getCount() - 1);
        totalHeight += dividerHeight;
        return totalHeight;
    }

    /**
     * re_calculate the height of gridview to show all data(it's bug for gridview and scrollview)
     *
     * @param gridView
     */
    public static void setGridViewHeightBasedOnChildren(GridView gridView, Context context, int verticalSpacingDp, int columes) {
        ListAdapter listAdapter = gridView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        float totalHeight = 0;
        int total = listAdapter.getCount();
        int rows = (int) Math.ceil(total / (float) columes);
        for (int i = 0; i < rows; i++) {
            View listItem = listAdapter.getView(i, null, gridView);
            listItem.measure(0, 0);
            float itemHeight = listItem.getMeasuredHeight();
            float space = UnitUtil.dip2px(context, verticalSpacingDp);
            totalHeight += itemHeight;
            totalHeight += space;
        }

        ViewGroup.LayoutParams params = gridView.getLayoutParams();
        params.height = (int) totalHeight;
        gridView.setLayoutParams(params);
    }

    /**
     * 设置viewpager滑动速度
     *
     * @param pager
     */
    public static void setViewPagerFlipSpeed(ViewPager pager) {
        if (pager != null) {
            Interpolator sInterpolator = new DecelerateInterpolator();
            try {
                Field mScroller;
                mScroller = ViewPager.class.getDeclaredField("mScroller");
                mScroller.setAccessible(true);
                FixedSpeedScroller scroller = new FixedSpeedScroller(pager.getContext(), sInterpolator);
                scroller.setDuration(AppConfig.VIEWPAGER_FLIP_SPEED);
                mScroller.set(pager, scroller);
            } catch (NoSuchFieldException e) {
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            }
        }
    }

}
